package api

import (
	"errors"
	"strconv"
	"strings"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type SubjectiveController struct {
	ApiBaseController
}

//拆分screening_id
func handleScreeningId(input string) (int64, int, error) {
	if len(input) <= 0 {
		return 0, 0, errors.New("参数错误")
	}
	var screening_id_s, quality_s string
	if strings.Index(input, "$") > -1 { //存在特殊符号
		split := strings.Split(input, "$")
		screening_id_s = split[0]
		quality_s = split[1]
	} else {
		screening_id_s = input
		quality_s = "0"
	}
	if len(screening_id_s) <= 0 {
		return 0, 0, errors.New("参数错误")
	}
	screening_id, err := strconv.ParseInt(screening_id_s, 10, 64)
	if err != nil {
		return 0, 0, err
	}
	quality, err := strconv.Atoi(quality_s)
	return screening_id, quality, nil
}
//E
type subjectivejson struct {
	Status enums.JsonResultCode `json:"status"`
	Errmsg string               `json:"errmsg"`
	Data   interface{}          `json:"result"`
}

func (c *SubjectiveController) jsonResultSubjectivejson(code enums.JsonResultCode, msg string, data interface{}) {
	r := &subjectivejson{code, msg, data}
	reset := make(map[string]*subjectivejson)
	reset["json"] = r
	c.Data["json"] = reset
	c.ServeJSON()
	c.StopRun()
}


//保存正常视力表数据
func (c *SubjectiveController) saveSubjectiveScreening(screening_id int64, isGlasses int, right, left string) error {
	screeningData, _ := models.GetScreeningDataByScreeningId(screening_id)
	if screeningData.Id == 0 {
		screeningData = &models.EquipmentData{}
		screeningData.Archives = &models.Archives{Id: screening_id}
	}
	if isGlasses == 0 { //祼眼
		screeningData.RightVision = right
		screeningData.LeftVision = left
	} else { //戴镜
		screeningData.RightCorrectVision = right
		screeningData.LeftCorrectVision = left
	}

	//if err := services.ScreeningDataSave(screeningData); err != nil {
	//	return err
	//}
	return nil
}


//保存正常视力表数据
func (c *SubjectiveController) saveSubjectiveScreeningV2(screening_id int64, isGlasses int, right, left, correct_right, correct_left string) error {
	screeningData := &models.EquipmentData{}
	screeningData.Archives = &models.Archives{Id: screening_id}

	screeningData.RightVision = right
	screeningData.LeftVision = left
	screeningData.RightCorrectVision = correct_right
	screeningData.LeftCorrectVision = correct_left

	//if err := services.ScreeningDataSave(screeningData); err != nil {
	//	return err
	//}
	//更新为NEW

	return nil
}

//保存视力表数据
func (c *SubjectiveController) Subjective() {

	userID := c.GetString("userID")       //获取USERID
	right_decimals := c.GetString("right_decimals")
	left_decimals := c.GetString("left_decimals")
	isGlasses, _ := c.GetInt("isGlasses") //是否戴镜

	data := make(map[string]string)
	if len(right_decimals) > 0 && len(left_decimals) > 0 { //ver2.0
		correct_right_decimals := c.GetString("correct_right_decimals")
		correct_left_decimals := c.GetString("correct_left_decimals")
		archives, err := models.GetArchivesByNumber(userID)
		if err != nil {
			c.jsonResultSubjectivejson(enums.JRCodeFailed, "保存失败", data)
		}
		if err := c.saveSubjectiveScreeningV2(archives.Id, isGlasses, right_decimals, left_decimals, correct_right_decimals, correct_left_decimals); err != nil {
			c.jsonResultSubjectivejson(enums.JRCodeFailed, "数据保存失败"+err.Error(), data)
		}

		sc := new(models.Archives)
		sc.Id = archives.Id
		//sc.IsNew = 1
		sc.ArchivesEdit("IsNew")

		c.jsonResultSubjectivejson(enums.JRCodeSucc, "保存成功", data)

	} else { //根据USERID获取用户基本信息
		ser := new(services.MssqlService)
		screening, err := ser.GetScreeningInfo(userID, models.BackendUser{})
		if err != nil {
			c.jsonResultSubjectivejson(enums.JRCodeFailed, "获取数据失败"+err.Error(), data)
		} else {
			data["userName"] = screening.Realname
			data["userClass"] = screening.Sex
			data["userID"] = strconv.FormatInt(screening.Id, 10)
		}
		c.jsonResultSubjectivejson(enums.JRCodeSucc, "", data)
	}

}

//保存验光仪数据[正常数据]
func (c *SubjectiveController) saveArchivesScreening(screening_id int64) error {
	sd, _ := models.GetScreeningDataByScreeningId(screening_id)
	if sd.Id == 0 {
		sd = new(models.EquipmentData)
	}
	if err := c.ParseForm(sd); err != nil {
		return errors.New("提交参数错误")
	}
	sc := new(models.Archives)
	sc.Id = screening_id;
	sd.Archives = sc

	//if err := services.ScreeningDataSave(sd); err != nil {
	//	return err
	//}
	return nil
}

//保存验光仪数据
func (c *SubjectiveController) SaveArchives() {
	archives_id := c.GetString("archives_id")
	screening_id, _, err := handleScreeningId(archives_id)
	if err != nil {
		c.jsonResultSubjectivejson(enums.JRCodeFailed, err.Error(), "")
	}
	if err := c.saveArchivesScreening(screening_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}